package org.awenlice.playerdailyshop.command;

import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.StringArgumentType;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.commands.arguments.EntityArgument;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerPlayer;
import net.minecraftforge.event.RegisterCommandsEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import org.awenlice.playerdailyshop.data.PlayerBuyShopData;
import org.awenlice.playerdailyshop.data.PlayerBuyShopDataManager;
import org.awenlice.playerdailyshop.data.PlayerShopData;
import org.awenlice.playerdailyshop.data.PlayerShopDataManager;
import org.awenlice.playerdailyshop.gui.BuyShopGui;
import org.awenlice.playerdailyshop.gui.ShopGui;

import java.util.Collection;

@Mod.EventBusSubscriber
public class ShopCommand {
    @SubscribeEvent
    public static void onRegisterCommands(RegisterCommandsEvent event) {
        CommandDispatcher<CommandSourceStack> dispatcher = event.getDispatcher();
        dispatcher.register(Commands.literal("shop")
                .requires(source -> source.hasPermission(Commands.LEVEL_GAMEMASTERS))
                .then(Commands.literal("reset")
                        .executes(context -> {
                            // 重置所有玩家的商店数据
                            resetAllPlayersShopData(context.getSource());
                            return 1;
                        })
                        .then(Commands.argument("player", EntityArgument.players())
                                .executes(context -> {
                                    // 重置指定玩家的商店数据
                                    Collection<ServerPlayer> players = EntityArgument.getPlayers(context, "player");
                                    resetPlayersShopData(context.getSource(), players);
                                    return 1;
                                })
                        )
                )
        );
    }

    /**
     * 重置所有玩家的商店数据
     */
    private static void resetAllPlayersShopData(CommandSourceStack source) {
        for (ServerPlayer player : source.getServer().getPlayerList().getPlayers()) {
            resetPlayerShopData(player);
        }
        source.sendSuccess(() -> Component.translatable("pds.resetAllPlayer"), false);
    }

    /**
     * 重置指定玩家的商店数据
     */
    private static void resetPlayersShopData(CommandSourceStack source, Collection<ServerPlayer> players) {
        for (ServerPlayer player : players) {
            resetPlayerShopData(player);
        }
        source.sendSuccess(() -> Component.translatable("pds.resetPlayer"), false);
    }

    /**
     * 重置单个玩家的商店数据
     */
    private static void resetPlayerShopData(ServerPlayer player) {
        // 重置出售商店数据
        PlayerShopData sellShopData = PlayerShopDataManager.get(player);
        sellShopData.setShopItems(ShopGui.generateShopItems());

        // 重置收购商店数据
        PlayerBuyShopData buyShopData = PlayerBuyShopDataManager.get(player);
        buyShopData.setBuyShopItems(BuyShopGui.generateBuyShopItems());
    }
}